#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import socket
from tkinter import Tk, Label, Button, Frame
from urllib.request import urlopen
from urllib.error import URLError


class IPApp:
    '''A simple GUI application to get WAN and local IP addresses.'''
    def __init__(self):
        '''Initialize the application'''
        self.root = Tk()
        self.root.title('Khaled programming practice')
        self._setup_ui()

    def _setup_ui(self) -> None:
        """Initialize the user interface"""
        # Result label
        self.res = Label(self.root, text='00.00.00.00', font=25)
        self.res.grid(row=0, column=0, columnspan=4, sticky='N', padx=10, pady=5)

        # Buttons
        Button(self.root, text='Get Wan IP', command=self.get_wan_ip).grid(
            row=1, column=0, padx=5, pady=5, sticky='W')
        Button(self.root, text='Get Local IP', command=self.get_local_ip).grid(
            row=1, column=1, padx=5, pady=5, sticky='W')
        Button(self.root, text='About', command=self.show_about).grid(
            row=1, column=2, padx=5, pady=5, sticky='W')
        Button(self.root, text='Quit', command=self.root.quit, bg='#f40').grid(
            row=1, column=3, padx=5, pady=5, sticky='E')

        # About section widgets (initially hidden)
        self.about_frame = Frame(self.root, width=350, height=2, bg='blue')
        self.about_info = Label(self.root, text="""\
Practice Python 
Take idea from here:
https://github.com/geekcomputers/Python/blob/master/myip.py
""", fg='#02F')
        self.about_close = Button(self.root, text='Close', 
                                 command=self.hide_about, bg='#55F')

    def get_wan_ip(self) -> None:
        """Get and display WAN IP address"""
        try:
            wan_ip = urlopen('http://ipecho.net/plain', timeout=5).read().decode('utf-8')
            self.res.configure(text=f'WAN IP is: {wan_ip}', fg='#600')
        except URLError as e:
            self.res.configure(text=f'Network error: {e.reason}', fg='red')
        except Exception as e:
            self.res.configure(text=f'Unexpected error: {str(e)}', fg='red')

    def get_local_ip(self) -> None:
        """Get and display local IP address"""
        try:
            local_ip = socket.gethostbyname(socket.gethostname())
            self.res.configure(text=f'Local IP is: {local_ip}', fg='#600')
        except Exception as e:
            self.res.configure(text=f'Error getting local IP: {str(e)}', fg='red')

    def show_about(self) -> None:
        """Show about information"""
        self.about_frame.grid(row=2, column=0, columnspan=4)
        self.about_info.grid(row=3, column=0, columnspan=4, padx=5)
        self.about_close.grid(row=4, column=0, columnspan=4, pady=5)

    def hide_about(self) -> None:
        """Hide about information"""
        self.about_frame.grid_remove()
        self.about_info.grid_remove()
        self.about_close.grid_remove()

    def run(self) -> None:
        """Start the application"""
        self.root.mainloop()


def main() -> None:
    app = IPApp()
    app.run()


if __name__ == '__main__':
    main()